/**
 * Created by YJH on 2018/6/7.
 */
'use strict'
/**
 * 功能：可以进行网络请求操作取消的功能
 * @param promise
 * @returns {{promise: Promise, cancel: (function())}}
 */
export default function makeCancelable(promise) {
    let hasCancelable_ = false;
    const wrappedPromise = new Promise((resolve, reject) => {
        promise.then(val => {
            hasCancelable_ ? reject({isCancled: true}) : resolve(val)
        });

        promise.catch(error => {
            hasCancelable_ ? reject({isCancled: true}) : resolve(error)
        })
    });

    return {
        promise: wrappedPromise,
        cancel(){
            hasCancelable_ = true;
        }
    }
}